package com.hdsx.restful.glzt.roadline.lxxx.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;

import com.esri.arcgis.geometry.IGeometry;
import com.hdsx.ao.base.BaseOpearte;
import com.hdsx.ao.util.GemotryUtil;
import com.hdsx.restful.base.PreparedDataListener;
import com.hdsx.restful.glzt.roadline.cfld.mapper.CfldMapper;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Line;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Lx;
import com.hdsx.restful.glzt.roadline.lxxx.bean.LxdmMapLxmc;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Xzqh;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Znode;
import com.hdsx.restful.glzt.roadline.lxxx.mapper.LxMapper;
import com.hdsx.restful.glzt.roadline.lxxx.service.LxService;
import com.hdsx.restful.glzt.structure.ql.bean.DoubleValueName;
import com.hdsx.restful.glzt.structure.ql.bean.IntValueName;
import com.hdsx.restful.glzt.structure.ql.bean.StatisticalResult;
import com.hdsx.restful.util.CheckEmpty;
import com.hdsx.restful.util.Dao;

@Service
public class LxServiceImpl extends BaseOpearte<Lx> implements LxService {
	public LxServiceImpl() {
		super("GIS_XZQH");//查询数据空间数据库表名
	}

	@Resource(name="lxMapper")
	private LxMapper mapper;
	@Resource(name="cfldMapper")
	private CfldMapper cfldMapper;
	@Override
	public Lx getById(String id) {
		return mapper.queryOne(id);
	}

	@Override
	public boolean addLx(Lx lx) {
		return mapper.add(lx)>0;
	}

	@Override
	public boolean updateLx(Lx lx) {
		return mapper.update(lx)>0;
	}

	@Override
	public boolean deleteById(String id) {
		return mapper.delete(id)>0;
	}


	@Override
	public int queryLxCountBylxdm(String lxdm) {
		Map<String,String> map=new HashMap<String, String>();
		map.put("lxdm",lxdm);
		return mapper.queryLxCountByLxdm(map);
	}

	@Override
	public List<Lx> queryLxBylxdm(String lxdm, int begin, int end) {
		
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("begin",begin);
		parameter.put("end", end);
		parameter.put("lxdm",lxdm);
		return mapper.queryLxBylxdm(parameter);
		
	}

	@Override
	public int queryLxCountByLxmc(String lxmc) {
		Map<String,String> map=new HashMap<String, String>();
		map.put("lxmc",lxmc);
		return mapper.queryLxCountByLxmc(map);
	}

	@Override
	public List<Lx> queryLxByLxmc(String lxmc, int begin, int end) {

		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("begin",begin);
		parameter.put("end", end);
		parameter.put("lxmc",lxmc);
		return mapper.queryByLxmc(parameter);
	}

	@Override
	public int queryLxCountByXzqhbm(String xzqhbm) {
		
		if(xzqhbm!=null){
			if(xzqhbm.endsWith("0000")){
				xzqhbm=xzqhbm.substring(0,2);
			}else if(xzqhbm.endsWith("00")){
				xzqhbm=xzqhbm.substring(0,4);
			}
		}
		
		Map<String,String> map=new HashMap<String, String>();
		map.put("xzqhbm",xzqhbm);
		return mapper.queryLxCountByXzqhbm(map);
	}

	@Override
	public List<Xzqh> queryLxByXzqhbm(String xzqhbm, int begin, int end) {
		
		if(xzqhbm!=null){
			if(xzqhbm.endsWith("0000")){
				xzqhbm=xzqhbm.substring(0,2);
			}else if(xzqhbm.endsWith("00")){
				xzqhbm=xzqhbm.substring(0,4);
			}
		}
		
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("begin",begin);
		parameter.put("end", end);
		parameter.put("xzqhbm",xzqhbm);
		return mapper.queryLxByXzqhbm(parameter);
	}

	@Override
	public int queryLxCountByParameter(String xzqhbm, String lxdm) {
		if(xzqhbm!=null){
			if(xzqhbm.endsWith("0000")){
				xzqhbm=xzqhbm.substring(0,2);
			}else if(xzqhbm.endsWith("00")){
				xzqhbm=xzqhbm.substring(0,4);
			}
		}
		
		Map<String,String> map=new HashMap<String, String>();
		map.put("xzqhbm",xzqhbm);
		map.put("lxdm",lxdm);
		return mapper.queryLxCountByParameter(map);
	}

	@Override
	public List<Xzqh> queryLxByParameter(String xzqhbm, String lxdm,
			 int begin, int end) {
		if(xzqhbm!=null){
			if(xzqhbm.endsWith("0000")){
				xzqhbm=xzqhbm.substring(0,2);
			}else if(xzqhbm.endsWith("00")){
				xzqhbm=xzqhbm.substring(0,4);
			}
		}
		
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("begin",begin);
		parameter.put("end", end);
		parameter.put("xzqhbm",xzqhbm);
		parameter.put("lxdm",lxdm);
		List<Xzqh> lists=mapper.queryLxByParameter(parameter);
		for(Xzqh xzqh:lists){
			xzqh.setShape(selectShape("ID='"+xzqh.getId()+"'"));
		}
		return lists; 
	}

	@Override
	public List<Lx> queryList(String lxdm) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("lxdm",lxdm);
		return mapper.queryList(parameter);
	}
	@Override
	public String queryLine(String id) {
		IGeometry shape=selectShape("ID='"+id+"'");
		return GemotryUtil.generateLineString(shape);
	}
	@Override
	public List<Dao> queryLineString(String id) {
		Lx lx=mapper.queryLineString(id);
		IGeometry shape=selectShape("ID= '"+id+"'");
		lx.setShape(shape);
		List<Dao> lists=new ArrayList<Dao>();
		lists.add(new Dao("路线代码",lx.getLxdm()));
		lists.add(new Dao("起点名称",lx.getQdmc()));
		lists.add(new Dao("起点桩号",lx.getQdzh()));
		lists.add(new Dao("止点桩号",lx.getZdzh()));
		lists.add(new Dao("止点名称",lx.getZdmc()));
		lists.add(new Dao("",lx.getZdmc()));
	   return lists;
	   
	}

	@Override
	public List<Lx> statisticsJsdjByLxdm(String lxdm) {
		
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("lxdm",lxdm);
 		List<Lx> lists=mapper.statisticsJsdjByLxdm(parameter);
		
		String[] arr={"一级公路","二级公路","三级公路","四级公路","高速公路","等外公路","其它"};
		List<String> str=new ArrayList<String>();
		Lx lx=null;
		
		for(Lx roadline:lists){
			if(roadline.getJsdj()==null){
				roadline.setJsdj("其它");
			}
			str.add(roadline.getJsdj());
		}
		
		for (String key : arr) {
			if(!str.contains(key)){
				lx=new Lx();
				lx.setJsdj(key);
				lx.setLens("0");
				lists.add(lx);
			}
		}
	
		return lists;
	}

	@Override
	public List<Lx> statisticsJsdjByXzqhbm(String xzqhbm) {
		if(xzqhbm!=null){
			if(xzqhbm.endsWith("0000")){
				xzqhbm=xzqhbm.substring(0,2);
			}else if(xzqhbm.endsWith("00")){
				xzqhbm=xzqhbm.substring(0,4);
			}
		}
		
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("xzqhbm",xzqhbm);
		 List<Lx> lists=mapper.statisticsJsdjByXzqhbm(parameter);
		String[] arr={"一级公路","二级公路","三级公路","四级公路","高速公路","等外公路","其它"};
		List<String> str=new ArrayList<String>();
		Lx lx=null;
		
		for(Lx roadline:lists){
			if(roadline.getJsdj()==null){
				roadline.setJsdj("其它");
			}
			str.add(roadline.getJsdj());
		}
		
		for (String key : arr) {
			if(!str.contains(key)){
				lx=new Lx();
				lx.setJsdj(key);
				lx.setLens("0");
				lists.add(lx);
			}
		}
	
		return lists;
		
	}

	@Override
	public List<Lx> statisticsXzdjByXzqhbm(String xzqhbm) {
		if(xzqhbm!=null){	
			if(xzqhbm.endsWith("0000")){
				xzqhbm=xzqhbm.substring(0,2);
			}else if(xzqhbm.endsWith("00")){
				xzqhbm=xzqhbm.substring(0,4);
			}
		}
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("xzqhbm",xzqhbm);
		
		List<Lx> lists=mapper.statisticsXzdjByXzqhbm(parameter);
		String[] arr={"国道","省道","县道","乡道","村道","专道","其它"};
		List<String> str=new ArrayList<String>();
		Lx lx=null;
		
		for(Lx roadline:lists){
			if(roadline.getXzdj()==null){
				roadline.setXzdj("其它");
			}
			str.add(roadline.getJsdj());
		}
		
		for (String key : arr) {
			if(!str.contains(key)){
				lx=new Lx();
				lx.setXzdj(key);
				lx.setLens("0");
				lists.add(lx);
			}
		}
	
		return lists;
		
	}

	@Override
	public List<Lx> statisticsCdtzByXzqhbm(String xzqhbm) {
		if(xzqhbm!=null){
			if(xzqhbm.endsWith("0000")){
				xzqhbm=xzqhbm.substring(0,2);
			}else if(xzqhbm.endsWith("00")){
				xzqhbm=xzqhbm.substring(0,4);
			}
		}
	
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("xzqhbm",xzqhbm);
		
		List<Lx> lists=mapper.statisticsCdtzByXzqhbm(parameter);
		
		String[] arr={"单车道","双车道","四车道","六车道","八车道","十车道","十二车道","其它"};
		List<String> str=new ArrayList<String>();
		Lx lx=null;
		
		for(Lx roadline:lists){
			if(roadline.getCdtz()==null){
				roadline.setCdtz("其它");
			}
			str.add(roadline.getCdtz());
		}
		
		for (String key : arr) {
			if(!str.contains(key)){
				lx=new Lx();
				lx.setCdtz(key);
				lx.setLens("0");
				lists.add(lx);
			}
		}
		
		return lists;
	}

	@Override
	public List<Lx> statisticsCdtzByLxdm(String lxdm) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("lxdm",lxdm);
		
		List<Lx> lists=mapper.statisticsCdtzByXzqhbm(parameter);
		
		String[] arr={"单车道","双车道","四车道","六车道","八车道","十车道","十二车道"};
		List<String> str=new ArrayList<String>();
		
		Lx lx=null;
		
		for(Lx roadline:lists){
			if(roadline.getCdtz()==null||roadline.getCdtz()==""){
				roadline.setCdtz("其它");
			}
			str.add(roadline.getCdtz());
		}
		
		for (String key : arr) {
			if(!str.contains(key)){
				lx=new Lx();
				lx.setCdtz(key);
				lx.setLens("0");
				lists.add(lx);
			}
		}
		
		return lists;
	}
	
	public StatisticalResult countLdByParameter(Map<String,Object> map) {
		List<IntValueName> seriesData = mapper.countLdTotalByParameter(map);
		List<DoubleValueName> seriesLengthData = mapper.countLdLengthByParameter(map);
		DecimalFormat df = new DecimalFormat("########.000");
		for(DoubleValueName dvn:seriesLengthData){
			if(dvn.getName().contains("高速")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldGs())));
			}else if(dvn.getName().contains("国道")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldGd())));
			}else if(dvn.getName().contains("省道")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldSd())));
			}else if(dvn.getName().contains("县道")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldXd())));
			}
		}
		List<String> legendData = mapper.selectLdClassify(map);
		StatisticalResult sr = new StatisticalResult();
		sr.setChartsText("路段统计");
		sr.setSeriesData(seriesData);
		sr.setSeriesLengthData(seriesLengthData);
		sr.setLegendData(legendData);
		return sr;
	}
	
	public StatisticalResult countLxByParameter(Map<String,Object> map) {
		List<IntValueName> seriesData = mapper.countTotalByParameter(map);
		List<DoubleValueName> seriesLengthData = mapper.countLengthByParameter(map);
		DecimalFormat df = new DecimalFormat("########.000");
		for(DoubleValueName dvn:seriesLengthData){
			if(dvn.getName().contains("高速")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldGs())));
			}else if(dvn.getName().contains("国道")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldGd())));
			}else if(dvn.getName().contains("省道")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldSd())));
			}else if(dvn.getName().contains("县道")){
				dvn.setValue(new Double(df.format(dvn.getValue() - cfldMapper.cfldXd())));
			}
		}
		List<String> legendData = new ArrayList<String>();
		for(IntValueName ivn : seriesData){
			legendData.add(ivn.getName());
		}
		StatisticalResult sr = new StatisticalResult();
		sr.setChartsText("路线统计");
		sr.setSeriesData(seriesData);
		sr.setSeriesLengthData(seriesLengthData);
		sr.setLegendData(legendData);
		return sr;
	}
	/**
	 * 联想输入
	 * 
	 * @param lx
	 * @return
	 */
	@Override
	public List<LxdmMapLxmc> selectResultsByParameter(String dm, String mc) {
		return mapper.selectResultsByParameter(dm, mc);
	}
	
	/**
	 * 行政等级查询节点 H G S X
	 * 
	 * @return
	 */
	@Override
	public List<Znode> xzdjNode() {
		List<Znode> xzdjNode = mapper.xzdjNode();
		return xzdjNode;
	}

	@Override
	public Lx queryByQzdzh(String lxdm,double qdzh, double zdzh) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("lxdm", lxdm);
		parameter.put("qdzh", qdzh);
		parameter.put("zdzh", zdzh);
		return mapper.queryByQzdzh(parameter);
	}

	@Override
	public List<Xzqh> querySQH() {
		Map<String,String> parameter1=new HashMap<String,String>();
		List<Xzqh> lists=mapper.querySQH(parameter1);
		List<Xzqh> xzqhs=null;
		
		for(Xzqh xzqh:lists){
			Map<String,String> parameter=new HashMap<String,String>();
			parameter.put("xzqhbm", xzqh.getXzqhbm());
			xzqhs=mapper.queryByXzqhbm(parameter);
			if(xzqhs!=null&&xzqhs.size()!=0){
				for(Xzqh item:xzqhs){
					IGeometry iGeometry=selectShape("ID='"+item.getId()+"'");
					item.setShape(iGeometry);
					item.setXzqhbm(xzqh.getXzqhbm());
					item.setXzqhmc(xzqh.getXzqhmc());
				}
			}
			
		}
		return xzqhs;
	}

	@Override
	public List<Xzqh> queryXQH(String xzqhbm, int page, int rows) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		if(xzqhbm!=null&&xzqhbm.endsWith("00")){
			xzqhbm.substring(0, 4);
		}
		parameter.put("xzqhbm", xzqhbm);
		parameter.put("begin", page);
		parameter.put("end", rows);
		List<Xzqh> lists=mapper.queryXQH(parameter);
		for(Xzqh item:lists){
			IGeometry iGeometry=selectShape("ID='"+item.getId()+"'");
			item.setShape(iGeometry);
			Map<String,String> parameter1=new HashMap<String,String>();
			parameter.put("xzqhbm", item.getXzqhbm());
			item.setXzqhmc(mapper.querySQH(parameter1).get(0).getXzqhmc());
		}
		return lists;
	}

	@Override
	public int queryCountXQH(String xzqhbm) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		parameter.put("xzqhbm", xzqhbm);
		return mapper.queryCountXQH(parameter);
	}

	@Override
	public Lx queryById(String id) {
		return mapper.selectById(id);
	}
	
	@Override
	public Line ldDetail(Line param) {
		Line line = new Line();
		if(!CheckEmpty.isEmptyString(param.getId())){
			Lx lx = mapper.selectById(param.getId());
			try {
				PropertyUtils.copyProperties(line, lx);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				line = null;
			}
		}else if(!CheckEmpty.isEmptyString(param.getLxdm())){
			Line detail = mapper.ldDetail(param);
			line.setLxdm(param.getLxdm());
			line.setQdzh(param.getQdzh());
			line.setZdzh(param.getZdzh());
			line.setXzqhbm(param.getXzqhbm());
			line.setSxxfx(param.getSxxfx());
			line.setLxmc(detail.getLxmc());
			line.setQdmc(detail.getQdmc());
			line.setZdmc(detail.getZdmc());
			line.setLc(detail.getLc());
		}
		return line;
	}
	
	/**
	 * 第三级数据
	 * @param line
	 * @return
	 */
	@Override
	public List<Line> third(){
		return mapper.third();
	}
	
	/**
	 * 第三级下一段
	 * @param line
	 * @return
	 */
	@Override
	public Line thirdnext(Line line){
		if(CheckEmpty.isEmptyString(line.getId()) || CheckEmpty.isEmptyString(line.getPid()))
			return null;
		List<Line> thirdLines = PreparedDataListener.getThirdLines();
		for(Line lx:thirdLines){
			if(lx.getId().equals(line.getId()) && lx.getPid().equals(line.getPid())){
				return lx;
			}
		}
		return null;
	}
	
	@Override
	public List<String> selectName(String name){
		return mapper.selectName(name);
	}
}
